package java44th.group.service.admin.log;

import com.github.pagehelper.PageHelper;
import java44th.group.bean.admin.bo.admin.CreateBo;
import java44th.group.bean.admin.bo.admin.UpdateBo;
import java44th.group.bean.admin.bo.order.OrderRefundBO;
import java44th.group.bean.admin.bo.order.OrderShipBO;
import java44th.group.bean.po.MarketLog;
import java44th.group.bean.admin.vo.log.LogList;
import java44th.group.mapper.MarketLogMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import java.sql.Timestamp;
import java.util.LinkedList;

/**
 * @Aurhor: 王可龙
 * @DATE: 2022/10/13 14:40
 * @Package:java44th.group.service.admin.log
 * @Description:
 * @Version 1.0
 */
@Service
public class MarketlogServiceImpl implements MarketlogService {

    @Autowired
    MarketLogMapper marketLogMapper;


    @Override
    public LogList list(Integer page, Integer limit, String name, String sort, String order) {
        PageHelper.startPage(page, limit);
        LinkedList<MarketLog> marketLogs = marketLogMapper.selectAllLogData(name);
        LogList logList = new LogList();
        Integer count = 0;
        if (name != null) {
            count = marketLogMapper.countByName(name);
        } else {
            count = Math.toIntExact(marketLogMapper.countByExample(null));
        }
        Integer pages = Math.toIntExact(count / limit);
        if (count % limit > 0) {
            pages++;
        }
        logList.setTotal(count);
        logList.setPages(pages);
        logList.setLimit(limit);
        logList.setPage(page);
        logList.setList(marketLogs);
        return logList;
    }

    @Override
    public void insertLog(ProceedingJoinPoint joinPoint, HttpServletRequest request,String username, String method) {
        Object[] args = joinPoint.getArgs();
        MarketLog marketLog = new MarketLog();
        marketLog.setAdmin(username);
        marketLog.setIp(request.getRemoteAddr());
        String result="";
        if (method.equals("login")) {
            marketLog.setAction("登录");
        }
        if (method.equals("logout")) {
            marketLog.setAction("退出");
        }
        if (method.equals("update")) {
            marketLog.setAction("编辑管理员");
            UpdateBo updateBo = (UpdateBo) args[0];
            result= updateBo.getUsername();
        }
        if (method.equals("delete")) {
            marketLog.setAction("删除管理员");
            UpdateBo updateBo = (UpdateBo) args[0];
            result= updateBo.getUsername();
        }
        if (method.equals("create")) {
            marketLog.setAction("添加管理员");
            CreateBo createBo = (CreateBo) args[0];
            result = createBo.getUsername();
        }
        if (method.equals("refund")) {
            marketLog.setAction("退款");
            OrderRefundBO orderRefundBO = (OrderRefundBO) args[0];
            Integer orderId = orderRefundBO.getOrderId();
            result= marketLogMapper.selectOrderSnById(orderId);
            result="订单编号  "+result;
        }
        if (method.equals("ship")) {
            marketLog.setAction("发货");
            OrderShipBO orderShipBO = (OrderShipBO) args[0];
            Integer orderId = orderShipBO.getOrderId();
            result= marketLogMapper.selectOrderSnById(orderId);
            result="订单编号  "+result;
        }
        // if (method.equals("deleteOrder")) {
        //     marketLog.setAction("删除");
        //     String orderId = (String) args[0];
        //     result= marketLogMapper.selectOrderSnById(Integer.valueOf(orderId));
        //     result="订单编号  "+result;
        // }
        marketLog.setType(1);
        marketLog.setStatus(true);
        marketLog.setResult(result);
        marketLog.setComment("");
        long l = System.currentTimeMillis();
        Timestamp s = new Timestamp(l);
        marketLog.setAddTime(s);
        marketLog.setUpdateTime(s);
        marketLog.setDeleted(false);
        int i = marketLogMapper.insertSelective(marketLog);
    }
}
